package com.luckynote.common.base.Filter;

import com.luckynote.common.base.config.AppConfig;
import com.luckynote.common.base.dubbo.LogEnum;
import org.slf4j.MDC;

import javax.servlet.*;
import java.io.IOException;
import java.util.UUID;

/**
 * <pre>
 * *********************************************
 * Copyright LUCKY-NOTE.
 * All rights reserved.
 * ****************************************************
 *  Version       Date         Author            Desc
 *    v1.0       2019/7/7      bingo        全链路日志校验过滤器
 * ****************************************************
 * </pre>
 */
public class GatherLogWebFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String traceId = UUID.randomUUID().toString().replace("-", "");
        MDC.put(LogEnum.TRACE_ID.getCode(),traceId);
        MDC.put(LogEnum.MODULE_NAME.getCode(), AppConfig.getProjectName());
        chain.doFilter(request,response);
        MDC.clear();
    }

    @Override
    public void destroy() {

    }
}
